<?php



include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");

include_once($_SERVER["DOCUMENT_ROOT"] . "/include/user/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/mblog/inc.php");

$g_cgival = array();
$g_pro = array();
$g_show = array();
if(! security_check::check_refer())
{
	throw new Exception("来路不明");
}

function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;
	
	cgi::both($g_cgival['t'], "t", "json");
		
	cgi::both($g_cgival['callback'], "callback", "");
	$g_cgival['callback'] = string::un_html(string::un_script_code($g_cgival['callback']));
	
	cgi::both($g_cgival["id"], "id", "");
	$g_cgival["id"] = string::un_html($g_cgival["id"]);
	
	
	cgi::both($g_cgival["type"], "type", "add");
}

function get_data()
{
	global $g_cgival, $g_pro, $g_show,$config;
	
	$type = $g_cgival["type"];
	$id = $g_cgival["id"];
	if(!$id)
	{
		throw new Exception("id不能为空！");
	}
	
	$user_info = new user_info();
	$is_login = $user_info->is_logined();
	
	if($is_login)
	{
		$user_detail_info = $user_info->get_cur_user_info();
		$uid = $user_detail_info["uid"];
		$user_nickname = $user_detail_info["user_nickname"];
		$g_show["current_user_info"] = $user_detail_info;
		$g_show["is_logined"] = true;
	}
	
	
	$ip = IP::get_client_ip();
	$db_mblog_user_love = new db_mblog_user_love();
	if($type == "add")
	{
		$key = $uid.$ip.$id;
		$crc32_key = sprintf("%u",crc32($key));
		$ret = $db_mblog_user_love->insert_mblog_user_love($id,$uid,$crc32_key);
		if($ret)
		{
			
			$db_mblog_pic_content = new db_mblog_pic_content();
			$item = $db_mblog_pic_content->get_mblog_pic_content_item($id);
			
			if(item)
			{
				//更新mblog
				$db_mblog_pic_content = new db_mblog_pic_content();
				$db_mblog_pic_content->update_mblog_info_by_id($id,"",1);
				
					
				//更新话题的喜欢数
				$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
				$db_mblog_cron_topic_tag->update_mblog_cron_topic_tag_count($item["topic_id"],1);
				
				
				if($is_login)
				{	
					//更新用户喜欢数
					$db_user_info = new db_user_info();
					$db_user_info->update_user_count_by_uid($uid,1);
					//更新feed
					$feed_data["create_time"] = time();
					$feed_data["content"] = $item["content"];
					$feed_data["create_uid"] = $user_detail_info["uid"];
					$feed_data["create_nickname"] = $user_detail_info["user_nickname"];
					$feed_data["user_pic"] = $user_detail_info["user_pic"];
					$feed_data["pic"][] = $item["pic"];
					$feed_data["id"][] = $item["id"];
					
				
					$db_user_feed = new db_user_feed();
					
					$res = $db_user_feed->get_feed_by_uid_and_type_today($uid,1);
					if($res)
					{
						$res= $res[0];
						$item_data_detail = unserialize($res["data_detail"]);
						
						
						$feed_data["pic"] = array_merge($feed_data["pic"],$item_data_detail["pic"]);
						
					
						$feed_data["id"] = array_merge($feed_data["id"],$item_data_detail["id"]);
						$feed_data["count_pic"] = count($feed_data["pic"]);
						
						$data_detail = serialize($feed_data);
						$db_user_feed->update_user_feed_by_id($res["id"],$data_detail);
						
					}
					else
					{
						$data_detail = serialize($feed_data);
						$db_user_feed->insert_user_feed($uid,$data_detail,1);
						
					}
				}
				
			
				
				
				
			}
			
			
			
			
		}
		else
		{
			throw new Exception("您已经喜欢过了！");
			
		}
		$g_show["errmsg"] = '喜欢+1';
		
		$db_mblog_reply = new db_mblog_reply();
		$love_content = "喜欢这个分享！";
		
		if($uid)
		{
			$res = $db_mblog_reply->insert_mblog_reply_info($uid,$user_detail_info["user_nickname"],$id,$love_content);
		
		}
		
		if(!$res)
		{
			//throw new Exception("数据操作失败");
		}
		else
		{
			$db_mblog_pic_content = new db_mblog_pic_content();
			$db_mblog_pic_content->update_mblog_info_by_id($id,"","",1);
		}
		
		
	}
	else
	{
		
		
		$ret = $db_mblog_user_love->update_mblog_user_love_by_uid_and_mblog_id($id,$uid,0);
		
		if($ret)
		{
		
			//更新用户喜欢数
			$db_user_info = new db_user_info();
			$db_user_info->update_user_count_by_uid($uid,-1);
			
		}
		$g_show["errmsg"] = '删除成功！';
	}
	
	
	
	

}

$g_show["error"] = 0;

try
{
	check_cgi_pro();
	get_data();
}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}

show_pro();
exit;


function show_pro()
{
	global $g_cgival, $g_pro, $g_show;
	if ($g_cgival['t'] == "serialize")
	{
		echo serialize($g_show);
	}
	elseif ($g_cgival['t'] == "json")
	{
		echo output::json("json", $g_show, $g_cgival['callback']);
	}
	elseif($g_cgival['t'] == "jsonp")
	{
		echo output::json("jsonp", $g_show, $g_cgival['callback']);
	}
	elseif($g_cgival['t'] == "perl")
	{
		$string = php_to_perl::php_array_to_perl_hash($g_show);
		$string = mb_convert_encoding($string, "GBK", "UTF-8");
		echo $string;
	}
	elseif($g_cgival['t'] == "iframe")
	{
		header("location:{$g_cgival['link']}");
	}
	else
	{
		echo output::json("jsonp", $g_show, $g_cgival['callback']);
	}
}


